package com.tancire.question.controller;

import com.tancire.common.annotation.Client;
import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.core.page.TableDataInfo;
import com.tancire.common.enums.UserSource;
import com.tancire.common.utils.DateUtils;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.member.domain.MemberTrainActivity;
import com.tancire.member.domain.MemberTrainAward;
import com.tancire.member.domain.MemberTrainProblem;
import com.tancire.member.service.IMemberTrainActivityService;
import com.tancire.member.service.IMemberTrainAwardService;
import com.tancire.member.service.IMemberTrainProblemService;
import com.tancire.question.domain.TrainActivity;
import com.tancire.question.domain.TrainActivityProblem;
import com.tancire.question.domain.TrainExam;
import com.tancire.question.domain.TrainProblem;
import com.tancire.question.service.ITrainActivityProblemService;
import com.tancire.question.service.ITrainActivityService;
import com.tancire.question.service.ITrainProblemService;
import com.tancire.question.service.ITrainProblemTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 答题活动Controller
 *
 * @author tancire
 * @date 2024-10-10
 */
@RestController
@RequestMapping("/question/mobile/train")
public class TrainActivityMemberController extends BaseController {

    @Resource
    private ITrainProblemTypeService trainProblemTypeService;

    @Resource
    private ITrainActivityService trainActivityService;

    @Resource
    private ITrainProblemService trainProblemService;

    @Resource
    private IMemberTrainProblemService memberTrainProblemService;

    @Resource
    private IMemberTrainAwardService memberTrainAwardService;

    @Resource
    private ITrainActivityProblemService trainActivityProblemService;

    @Resource
    private IMemberTrainActivityService memberTrainActivityService;


    /**
     * 构建测试题
     */
    @Client(UserSource.MEMBER)
    @PostMapping("/build/exam")
    public AjaxResult buildExam(@RequestBody TrainExam trainExam) {
        List<TrainProblem> list = trainProblemService.buildExam(trainExam);
        return AjaxResult.success(list);
    }

    /**
     * 加入错题本
     */
    @Client(UserSource.MEMBER)
    @PostMapping("/add/error")
    public AjaxResult addErrorProblem(@RequestBody TrainProblem trainProblem) {
        trainProblemService.addErrorProblem(trainProblem);
        return AjaxResult.success();
    }

    /**
     * 从错题本移除
     */
    @Client(UserSource.MEMBER)
    @GetMapping("/remove/error/{id}")
    public AjaxResult removeErrorProblem(@PathVariable("id") Long id) {
        trainProblemService.removeErrorProblem(id);
        return AjaxResult.success();
    }

    /**
     * 获取我的错题本
     */
    @Client(UserSource.MEMBER)
    @GetMapping("/mine/error/list")
    public TableDataInfo getErrorProblem(MemberTrainProblem memberTrainProblem) {
        memberTrainProblem.setMemberId(SecurityUtils.getUserId());
        startPage();
        List<MemberTrainProblem> memberTrainProblemList = memberTrainProblemService.selectMemberTrainProblemList(memberTrainProblem);
        return getDataTable(memberTrainProblemList);
    }

    /**
     * 获取我的奖品列表
     */
    @Client(UserSource.MEMBER)
    @GetMapping("/mine/award/list")
    public TableDataInfo getAwardList(MemberTrainAward memberTrainAward) {
        memberTrainAward.setMemberId(SecurityUtils.getUserId());
        startPage();
        List<MemberTrainAward> memberTrainAwards = memberTrainAwardService.selectMemberTrainAwardList(memberTrainAward);
        return getDataTable(memberTrainAwards);
    }

    /**
     * 查询答题活动列表
     */
    @Client(UserSource.MEMBER)
    @GetMapping("/activity/list")
    public TableDataInfo activityList(TrainActivity trainActivity) {
        startPage();
        List<TrainActivity> list = trainActivityService.selectTrainActivityList(trainActivity);
        return getDataTable(list);
    }

    /**
     * 开始答题接口
     */
    @Client(UserSource.MEMBER)
    @GetMapping("/start/activity/{activityId}")
    public AjaxResult startAnswerActivity(@PathVariable("activityId") Long activityId) {
        MemberTrainActivity memberTrainActivity = this.memberTrainActivityService.startActivity(activityId);
        return AjaxResult.success("开启成功", memberTrainActivity);
    }

    /**
     * 答题交卷接口
     */
    @Client(UserSource.MEMBER)
    @PostMapping("/activity/answer/submit")
    public AjaxResult answerSubmit(@RequestBody MemberTrainActivity memberTrainActivity) {
        this.memberTrainActivityService.answerSubmit(memberTrainActivity);
        return AjaxResult.success("交卷成功");
    }

    /**
     * 查询已参加的活动列表
     */
    @Client(UserSource.MEMBER)
    @GetMapping("/activity/attend/list")
    public AjaxResult getAttendList() {
        List<String> ids = this.memberTrainActivityService.getAttendList();
        return AjaxResult.success("开启成功", ids);
    }

    /**
     * 查询活动题目列表
     */
    @Client(UserSource.MEMBER)
    @GetMapping("/activity/problem/list")
    public AjaxResult activityProblemList(TrainActivityProblem trainActivityProblem) {
        // 限制查询题目
        List<TrainActivityProblem> list = new ArrayList<>();
        if (null != trainActivityProblem.getActivityId()) {
            TrainActivity trainActivity = trainActivityService.selectTrainActivityById(trainActivityProblem.getActivityId());
            if (DateUtils.timeBetween(trainActivity.getActivityStart(), trainActivity.getActivityEnd())) {
                list = trainActivityProblemService.selectTrainActivityProblemList(trainActivityProblem);
            }
        }
        return AjaxResult.success(list);
    }




}
